package com.apartmentManage.service.impl;


import com.apartmentManage.Dao.AdminDao;
import com.apartmentManage.entity.Admin;
import com.apartmentManage.service.AdminService;
import com.apartmentManage.until.IDRandom;
import com.apartmentManage.until.StringUntil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    private AdminDao dao;

    @Override
    public boolean getByRequirement(Admin admin) {
        return dao.getByRequirement(admin)==null ? false : true;
    }

    @Override
    public boolean save(Admin admin,String againPassword) {
        if (StringUntil.IsBlank(admin.getPassword())
        || StringUntil.IsBlank(admin.getAccountName())
        || StringUntil.IsBlank(againPassword))
            return false;
        if (!(againPassword.equals(admin.getPassword())))
            return false;
        Admin result = dao.getByRequirement(admin);
        if (result!=null)
            return false;
//        获取随机NO
        String no = IDRandom.getId(dao.getAllNo());
        admin.setNo(no);
        return dao.save(admin)>0;
    }

    @Override
    public boolean updateByOldPassword(Admin admin) {
        if ( StringUntil.IsBlank(admin.getPassword())
                ||StringUntil.IsBlank(admin.getOldPassword())
              ||  admin.getPassword().equals(admin.getOldPassword()))
            return false;
        return dao.updateByOldPassword(admin)>0;
    }
}
